//
//  WhaleListView.swift
//  IndustryNet
//
//  Created by topnet on 2022/1/21.
//

import UIKit

import RxCocoa
import MJRefresh



class WhaleListView: UITableView {
    var refreshClosure: MJRefreshComponentAction? {
        didSet {
            if refreshClosure == nil {
                return
            }
            mj_header = MJRefreshNormalHeader(refreshingBlock: refreshClosure!)
        }
    }
    var loadingClosure: MJRefreshComponentAction? {
        didSet {
            if loadingClosure == nil {
                return
            }
            mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: loadingClosure!)
        }
    }
    
    private lazy var disposeBag = DisposeBag()
    private var cellId = "CellId"
    private var data: [TableCellModelProtocol]?

    init() {
        super.init(frame: .zero, style: .grouped)
        contentInsetAdjustmentBehavior = .never
        delegate = self
    }
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        contentInsetAdjustmentBehavior = .never
        delegate = self
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configData<T: TableCellModelProtocol, U: WhaleListCell>(_ data: [T]?,
                                                             cellType: U.Type,
                                           cellForRow: ((Int, T, U)->())? = nil,
                                           itemSelected: @escaping ((Int, T)->())) {
        defer {
            reloadData()
        }
        dataSource = nil
        self.data = data
        guard let list = data else { return }
        register(U.classForCoder(), forCellReuseIdentifier: cellId)
        let dataSource = Observable.just(list)
        dataSource.bind(to:
                            rx.items(cellIdentifier: cellId, cellType: cellType))
        { row, mod, cell in
            cell.mod = mod
            cellForRow?(row, mod, cell)
        }.disposed(by: disposeBag)
        
        rx.itemSelected.subscribe(onNext: { [weak self]  e in
            guard let mod = self?.data?[e.item] else { return }
            itemSelected(e.item, mod as! T)
        }).disposed(by: disposeBag)
        
        
        
    }
    
}

extension WhaleListView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return UIView()
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        return UIView()
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 0
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.data?[indexPath.row].rowHeight ?? 50
    }
}
